﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Module_File_Detail : DetailPageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        base.initializeWebForm();

        if (!IsPostBack)
        {
            Genesis.Client.Common.Classes.LookupProvider lookupProvider = new Genesis.Client.Common.Classes.LookupProvider();

            System.Data.DataTable fileTypeTable = lookupProvider.GetLookupValues("Lookup_FileType", "FileTypeId", "FileTypeName", "GenesisConnectionString");

            fileTypeDropDownList.DataSource = fileTypeTable;
            fileTypeDropDownList.DataTextField = "Display";
            fileTypeDropDownList.DataValueField = "Value";

            fileTypeDropDownList.DataBind();

            Genesis.Client.Module.Classes.ModuleProvider moduleProvider = new Genesis.Client.Module.Classes.ModuleProvider();

            moduleProvider.SessionGuid = this.sessionGuid;

            System.Data.DataTable applicationTable = moduleProvider.ApplicationGetList();

            applicationDropDownList.DataSource = applicationTable;
            applicationDropDownList.DataTextField = "ApplicationName";
            applicationDropDownList.DataValueField = "ApplicationGuid";

            applicationDropDownList.DataBind();

            Guid _fileGuid = Guid.Empty;

            if (Request.QueryString["FileGuid"] != null)
            {
                _fileGuid = new Guid(Request.QueryString["FileGuid"]);
            }

            if (_fileGuid != Guid.Empty)
            {
                System.Data.DataTable fileTable = moduleProvider.FileGetByFileGuid(_fileGuid);

                if (fileTable.Rows.Count == 1)
                {
                    System.Data.DataRow fileRow = fileTable.Rows[0];

                    fileIdTextBox.Text = System.Convert.ToString(fileRow["FileId"]);
                    fileGuidTextBox.Text = System.Convert.ToString(fileRow["FileGuid"]);
                    orderTextBox.Text = System.Convert.ToString(fileRow["FileOrder"]);

                    fileNameTextBox.Text = System.Convert.ToString(fileRow["FileName"]);
                    fileDescriptionTextBox.Text = System.Convert.ToString(fileRow["FileDescription"]);

                    pathTextBox.Text = System.Convert.ToString(fileRow["FilePath"]);
                    urlTextBox.Text = System.Convert.ToString(fileRow["FileUrl"]);

                    versionMajorTextBox.Text = System.Convert.ToString(fileRow["FileVersionMajor"]);
                    versionMinorTextBox.Text = System.Convert.ToString(fileRow["FileVersionMinor"]);
                    versionBuildTextBox.Text = System.Convert.ToString(fileRow["FileVersionBuild"]);
                    versionRevisionTextBox.Text = System.Convert.ToString(fileRow["FileVersionRevision"]);

                    int _fileTypeId = System.Convert.ToInt32(fileRow["FileTypeId"]);
                    foreach (ListItem fileTypeItem in fileTypeDropDownList.Items)
                    {
                        if (System.Convert.ToInt32(fileTypeItem.Value) == _fileTypeId)
                        {
                            fileTypeItem.Selected = true;
                            break;
                        }
                    }

                    string _applicationGuidString = System.Convert.ToString(fileRow["ApplicationGuid"]);
                    foreach (ListItem applicationItem in applicationDropDownList.Items)
                    {
                        if (applicationItem.Value == _applicationGuidString)
                        {
                            applicationItem.Selected = true;
                            break;
                        }
                    }
                }
            }

            switch (formMode)
            {
                case FormMode.Create:
                    fileIdTextBox.Enabled = true;
                    fileGuidTextBox.Enabled = true;
                    orderTextBox.Enabled = true;
                    fileNameTextBox.Enabled = true;
                    fileDescriptionTextBox.Enabled = true;
                    pathTextBox.Enabled = true;
                    urlTextBox.Enabled = true;
                    versionMajorTextBox.Enabled = true;
                    versionMinorTextBox.Enabled = true;
                    versionBuildTextBox.Enabled = true;
                    versionRevisionTextBox.Enabled = true;

                    fileTypeDropDownList.Enabled = true;
                    applicationDropDownList.Enabled = true;

                    fileIdTextBox.ReadOnly = false;
                    fileGuidTextBox.ReadOnly = false;
                    orderTextBox.ReadOnly = false;
                    fileNameTextBox.ReadOnly = false;
                    fileDescriptionTextBox.ReadOnly = false;
                    pathTextBox.ReadOnly = false;
                    urlTextBox.ReadOnly = false;
                    versionMajorTextBox.ReadOnly = false;
                    versionMinorTextBox.ReadOnly = false;
                    versionBuildTextBox.ReadOnly = false;
                    versionRevisionTextBox.ReadOnly = false;

                    deleteImageButton.Visible = false;
                    break;
                case FormMode.Delete:
                    fileIdTextBox.Enabled = true;
                    fileGuidTextBox.Enabled = true;
                    orderTextBox.Enabled = true;
                    fileNameTextBox.Enabled = true;
                    fileDescriptionTextBox.Enabled = true;
                    pathTextBox.Enabled = true;
                    urlTextBox.Enabled = true;
                    versionMajorTextBox.Enabled = true;
                    versionMinorTextBox.Enabled = true;
                    versionBuildTextBox.Enabled = true;
                    versionRevisionTextBox.Enabled = true;

                    fileTypeDropDownList.Enabled = false;
                    applicationDropDownList.Enabled = false;

                    fileIdTextBox.ReadOnly = true;
                    fileGuidTextBox.ReadOnly = true;
                    orderTextBox.ReadOnly = true;
                    fileNameTextBox.ReadOnly = true;
                    fileDescriptionTextBox.ReadOnly = true;
                    pathTextBox.ReadOnly = true;
                    urlTextBox.ReadOnly = true;
                    versionMajorTextBox.ReadOnly = true;
                    versionMinorTextBox.ReadOnly = true;
                    versionBuildTextBox.ReadOnly = true;
                    versionRevisionTextBox.ReadOnly = true;

                    deleteImageButton.Visible = true;
                    break;
                case FormMode.Edit:
                    fileIdTextBox.Enabled = true;
                    fileGuidTextBox.Enabled = true;
                    orderTextBox.Enabled = true;
                    fileNameTextBox.Enabled = true;
                    fileDescriptionTextBox.Enabled = true;
                    pathTextBox.Enabled = true;
                    urlTextBox.Enabled = true;
                    versionMajorTextBox.Enabled = true;
                    versionMinorTextBox.Enabled = true;
                    versionBuildTextBox.Enabled = true;
                    versionRevisionTextBox.Enabled = true;

                    fileTypeDropDownList.Enabled = true;
                    applicationDropDownList.Enabled = true;

                    fileIdTextBox.ReadOnly = false;
                    fileGuidTextBox.ReadOnly = false;
                    orderTextBox.ReadOnly = false;
                    fileNameTextBox.ReadOnly = false;
                    fileDescriptionTextBox.ReadOnly = false;
                    pathTextBox.ReadOnly = false;
                    urlTextBox.ReadOnly = false;
                    versionMajorTextBox.ReadOnly = false;
                    versionMinorTextBox.ReadOnly = false;
                    versionBuildTextBox.ReadOnly = false;
                    versionRevisionTextBox.ReadOnly = false;

                    deleteImageButton.Visible = false;
                    break;
                case FormMode.Default:
                    fileIdTextBox.Enabled = true;
                    fileGuidTextBox.Enabled = true;
                    orderTextBox.Enabled = true;
                    fileNameTextBox.Enabled = true;
                    fileDescriptionTextBox.Enabled = true;
                    pathTextBox.Enabled = true;
                    urlTextBox.Enabled = true;
                    versionMajorTextBox.Enabled = true;
                    versionMinorTextBox.Enabled = true;
                    versionBuildTextBox.Enabled = true;
                    versionRevisionTextBox.Enabled = true;

                    fileTypeDropDownList.Enabled = false;
                    applicationDropDownList.Enabled = false;

                    fileIdTextBox.ReadOnly = true;
                    fileGuidTextBox.ReadOnly = true;
                    orderTextBox.ReadOnly = true;
                    fileNameTextBox.ReadOnly = true;
                    fileDescriptionTextBox.ReadOnly = true;
                    pathTextBox.ReadOnly = true;
                    urlTextBox.ReadOnly = true;
                    versionMajorTextBox.ReadOnly = true;
                    versionMinorTextBox.ReadOnly = true;
                    versionBuildTextBox.ReadOnly = true;
                    versionRevisionTextBox.ReadOnly = true;

                    deleteImageButton.Visible = false;
                    break;
            }
        }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect(this.buildForwardUrl("Default.aspx"));
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        Genesis.Client.Module.Classes.ModuleProvider moduleProvider = new Genesis.Client.Module.Classes.ModuleProvider();

        if (formMode == FormMode.Edit)
        {
            if (!moduleProvider.FileUpdate(
                new Guid(fileGuidTextBox.Text),
                System.Convert.ToInt32(fileIdTextBox.Text),
                new Guid(applicationDropDownList.SelectedValue),
                fileNameTextBox.Text,
                fileDescriptionTextBox.Text,
                pathTextBox.Text,
                urlTextBox.Text,
                System.Convert.ToInt32(fileTypeDropDownList.SelectedValue),
                System.Convert.ToInt32(versionMajorTextBox.Text),
                System.Convert.ToInt32(versionMinorTextBox.Text),
                System.Convert.ToInt32(versionBuildTextBox.Text),
                System.Convert.ToInt32(versionRevisionTextBox.Text),
                System.Convert.ToInt32(orderTextBox.Text)))
            {
                Response.Write("alert(\"An error occured while updating the database\");");
            }
            else
            {
                Response.Redirect(this.buildForwardUrl("Default.aspx"));
            }
        }
        else if (formMode == FormMode.Create)
        {
            if (moduleProvider.FileInsert(
                Guid.NewGuid(),
                new Guid(applicationDropDownList.SelectedValue),
                fileNameTextBox.Text,
                fileDescriptionTextBox.Text,
                pathTextBox.Text,
                urlTextBox.Text,
                System.Convert.ToInt32(fileTypeDropDownList.SelectedValue),
                System.Convert.ToInt32(versionMajorTextBox.Text),
                System.Convert.ToInt32(versionMinorTextBox.Text),
                System.Convert.ToInt32(versionBuildTextBox.Text),
                System.Convert.ToInt32(versionRevisionTextBox.Text),
                System.Convert.ToInt32(orderTextBox.Text)) == 0)
            {
                Response.Write("alert(\"An error occured while creating database entry\");");
            }
            else
            {
                Response.Redirect(this.buildForwardUrl("Default.aspx"));
            }
        }
    }
}
